// 导入axios
import axios from "axios";
// 单独引入Message
// import { Message } from 'element-ui';
import {Notification} from "element-ui";
// 导入路由
import router from "../router";

// axios请求拦截器
axios.interceptors.request.use(config => {
    // 如果存在token,请求携带token
    if(window.sessionStorage.getItem('tokenStr')){
        config.headers['Authorization'] = window.sessionStorage.getItem('tokenStr');
    }
    return config;
},error => {
    // 打印错误信息
    console.log(error);
});

// axios响应拦截器
axios.interceptors.response.use(success => {
    // 已经success调用后端接口,但是业务逻辑错误
    if(success.status && success.status == 200){
        // 500业务逻辑错误，401未登录，403权限不足
        if(success.data.code == 500 || success.data == 401 || success.data == 403){
            // Message.error({showClose: true,message:success.data.message});
            Notification({
                title: '😅',
                message: success.data.message,
                offset: 100,
                type: "warning",
            });
            return;
        }
        if(success.data.message){
            // Message.success({showClose: true,message:success.data.message});
            Notification({
                title: '😄',
                message: success.data.message,
                offset: 100,
                type: "success",
                duration: 666
            });
        }
    }
    return success.data;
},error => {
    // 504服务器有问题 404页面丢失
    if(error.response.code == 504 || error.response.code == 404){
        // Message.success({message:'服务器西内了o(╥﹏╥)o'});
        Notification({
            title: '😱',
            message: '服务器西内了o(╥﹏╥)o',
            offset: 100,
            type: "error",
        });
    }else if(error.response.code == 403){
        // Message.success({message:'权限不足,请联系管理员!'});
        Notification({
            title: '😝',
            message: '权限不足,请联系管理员!',
            offset: 100,
            type: "error",
        });
    }else if(error.response.code == 401){
        // Message.success({message:'尚未登录,请先登录!'});
        Notification({
            title: '🤔',
            message: '尚未登录,请先登录!',
            offset: 100,
            type: "error"
        });
        // 直接跳转登录页 -> /
        router.replace('/');
    }else{
        if(error.response.data.message){
            // Message.error({message:error.response.data.message});
            Notification({
                title: '🙋‍♀️🙋‍♂️‍',
                message: error.response.data.message,
                offset: 100,
                type: "error"
            });
        }else{
            // Message.error({message:'未知错误!'});
            Notification({
                title: '😨',
                message: '未知错误!',
                offset: 100,
                type: "error"
            });
        }
    }
    // return;
});

// 项目前缀预留
// var base = '';

// 传送json格式的get请求
export const getRequest = (url,params) => {
    return axios({
        method: 'get',
        // url: '${base}${url}',
        url: url,
        data: params
    })
}

// 传送json格式的post请求
export const postRequest = (url,params) => {
    return axios({
        method: 'post',
        // url: '${base}${url}',
        url: url,
        data: params
    })
}

// 传送json格式的put请求
export const putRequest = (url,params) => {
    return axios({
        method: 'put',
        // url: '${base}${url}',
        url: url,
        data: params
    })
}

// 传送json格式的delete请求
export const deleteRequest = (url,params) => {
    return axios({
        method: 'delete',
        // url: '${base}${url}',
        url: url,
        data: params
    })
}